﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Input;
using CPETwit.WPFClient.Business.Service;
using CPETwit.WPFClient.Business.TwitterServiceReference;
using CPETwit.WPFClient.Command;
using CPETwit.WPFClient.Common;
using CPETwit.WPFClient.Helper;
using MessageBox = System.Windows.MessageBox;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;

namespace CPETwit.WPFClient.ViewModel
{
    public class ProfileEditingViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        private string _email;
        private ICommand _saveCommand;
        private ICommand _cancelCommand;
        private ICommand _browseImageCommand;
        private string _name;
        private string _login;
        private string _password;
        private ImageSource _imageSource;

        private readonly WCFServiceCaller _serviceCaller;
        private DateTime? _birthDate;
        private string _sex;
        private string _description;
        private string _web;

        public ImageSource ImageSource
        {
            get { return _imageSource; }
            set
            {
                _imageSource = value;
                OnPropertyChanged("ImageSource");
            }
        }
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }
        public string Email
        {
            get { return _email; }
            set
            {
                _email = value;
                OnPropertyChanged("Email");
            }
        }
        public string Login
        {
            get { return _login; }
            set
            {
                _login = value;
                OnPropertyChanged("Login");
            }
        }
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                OnPropertyChanged("Password");
            }
        }
        public DateTime? BirthDate
        {
            get { return _birthDate; }
            set
            {
                _birthDate = value;
                OnPropertyChanged("BirthDate");
            }
        }
        public string Sex
        {
            get { return _sex; }
            set
            {
                _sex = value;
                OnPropertyChanged("Sex");
            }
        }
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                OnPropertyChanged("Description");
            }
        }
        public string Web
        {
            get { return _web; }
            set
            {
                _web = value;
                OnPropertyChanged("Web");
            }
        }
        public ICommand SaveCommand
        {
            get
            {
                return _saveCommand ?? (_saveCommand = new RelayCommand(param => Save(),
                                                                        param => CanSave()));
            }
        }
        public ICommand CancelCommand
        {
            get
            {
                return _cancelCommand ?? (_cancelCommand = new RelayCommand(param => Cancel(),
                                                                               param => CanCancel()));
            }
        }
        public ICommand BrowseImageCommand
        {
            get
            {
                return _browseImageCommand ??
                       (_browseImageCommand = new RelayCommand(param => BrowseImage(),
                                                               param => CanBrowseImage()));
            }
        }

        public ProfileEditingViewModel()
        {
            if (App.IsInDesignMode())
            {
                UpdateViewModel(new User
                         {
                             name = "Pierre Gillon",
                             email = "gillon.pierre69@gmail.com",
                             login = "pgillon",
                             password = "Bonjour",
                             web = "www.google.fr",
                             description = "Je m'appelle henri, j'aimerais réussir ma vie...",
                             sex = "M",
                             birthdate = DateTime.Now,
                             avatar = null
                         });
            }
            else
            {
                if (CommonData.LOGGED_USER != null)
                    UpdateViewModel(CommonData.LOGGED_USER);
            }

            _serviceCaller = CommonData.GetServiceCaller();
        }

        public void Save()
        {
            CommonData.LOGGED_USER.avatar = ImageLoader.GetBytesFromImageSource(ImageSource);
            CommonData.LOGGED_USER.name = Name;
            CommonData.LOGGED_USER.login = Login;
            CommonData.LOGGED_USER.password = Password;
            CommonData.LOGGED_USER.email = Email;
            CommonData.LOGGED_USER.birthdate = BirthDate;
            CommonData.LOGGED_USER.sex = Sex;
            CommonData.LOGGED_USER.description = Description;
            CommonData.LOGGED_USER.web = Web;

            _serviceCaller.UpdateUser(CommonData.LOGGED_USER);
            MessageBox.Show("Votre profil a bien été sauvegardé.", "Sauvegarde");
            DialogManager.UpdateProfileDisplaying(CommonData.LOGGED_USER);
            DialogManager.UpdateTweetMenus();
            DialogManager.ChangeBackGround();
        }
        public bool CanSave()
        {
            return string.IsNullOrEmpty(Name) == false &&
                   string.IsNullOrEmpty(Email) == false &&
                   string.IsNullOrEmpty(Login) == false &&
                   string.IsNullOrEmpty(Password) == false;
        }
        public void Cancel()
        {

        }
        public bool CanCancel()
        {
            return true;
        }
        public void BrowseImage()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;
            dialog.Filter = "Fichiers images (*.bmp, *.jpg, *.png, *.gif)|*.bmp;*.jpg;*.png;*.gif|All files (*.*)|*.*";
            if (dialog.ShowDialog() == true)
            {
                Stream stream = dialog.OpenFile();
                if (stream.Length > MaxFileSize)
                    MessageBox.Show("La taille de l'image est trop importante.", "Choix d'une image",
                                    MessageBoxButton.OK, MessageBoxImage.Warning);
                else
                    ImageSource = ImageLoader.GetImageSourceFromStream(stream);
            }
        }

        private const int MaxFileSize = 700*1024*1024;

        public bool CanBrowseImage()
        {
            return true;
        }

        public void UpdateViewModel(User user)
        {
            ImageSource = CommonData.GetImageSourceFromAvatar(user.avatar);
            Name = user.name;
            Login = user.login;
            Email = user.email;
            Password = user.password;
            BirthDate = user.birthdate;
            Sex = user.sex;
            Description = user.description;
            Web = user.web;
        }


    }
}
